package com.tzpay.tzpay.view;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.tzpay.tzpay.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yamlee on 15/8/14.
 */
public class AppBar extends FrameLayout {
    @Bind(R.id.tv_navigation_header_left)
    TextView tvNavigationHeaderLeft;
    @Bind(R.id.iv_navigation_header_left)
    ImageView ivNavigationHeaderLeft;
    @Bind(R.id.tv_naviation_title)
    TextView tvNaviationTitle;
    @Bind(R.id.tv_navigation_header_right)
    TextView tvNavigationHeaderRight;
    @Bind(R.id.iv_navigation_header_right)
    ImageView ivNavigationHeaderRight;
    @Bind(R.id.layout_navigation_header)
    RelativeLayout layoutNavigationHeader;
    @Bind(R.id.iv_title_image)
    ImageView ivTitleImage;
    @Bind(R.id.rl_navigation_left)
    RelativeLayout rlNavigationLeft;
    @Bind(R.id.rl_navigation_right)
    RelativeLayout rlNavigationRight;
    @Bind(R.id.ll_title)
    LinearLayout llTitle;
    @Bind(R.id.sp_date)
    Spinner spDate;
    @Bind(R.id.iv_trade_list_down_arrow)
    ImageView ivTradeListDownArrow;
    @Bind(R.id.rl_title_spinner)
    RelativeLayout rlTitleSpinner;
    @Bind(R.id.iv_base_divider)
    ImageView ivBaseDivider;

    private Context mContext;

    private View view;
    public static final int TITEL_TYPE_SPINNER = 11;
    public static final int TITLE_TYPE_WITH_ARROW = 12;
    public static final int TITLE_TYPE_JUST_TITLE = 13;


    public AppBar(Context mContext) {
        super(mContext);
        initView(mContext);
    }

    public AppBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public AppBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        this.mContext = context;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        view = inflater.inflate(R.layout.include_layout_heaer, this);
        ButterKnife.bind(view);
    }

    public void setLeftNavigation(String leftTitle, final OnLeftClickListener onLeftClickListener) {
        tvNavigationHeaderLeft.setVisibility(View.VISIBLE);
        ivNavigationHeaderLeft.setVisibility(View.GONE);
        tvNavigationHeaderLeft.setText(leftTitle);
        rlNavigationLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onLeftClickListener.onClick(v);
            }
        });
    }

    public void setLeftNavigation(int imgResId, final OnLeftClickListener onLeftClickListener) throws NotFoundException {
        Drawable drawable = mContext.getResources().getDrawable(imgResId);
        if (null == drawable) throw new NotFoundException("thre resoruce id not found in drawable");
        ivNavigationHeaderLeft.setVisibility(View.VISIBLE);
        tvNavigationHeaderLeft.setVisibility(View.GONE);
        ivNavigationHeaderLeft.setImageResource(imgResId);
        rlNavigationLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onLeftClickListener.onClick(v);
            }
        });
    }


    public void setRightNavigation(String rightTitle, final OnRightClickListener onRightClickListener) {
        tvNavigationHeaderRight.setVisibility(View.VISIBLE);
        tvNavigationHeaderRight.setText(rightTitle);
        rlNavigationRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onRightClickListener.onClick(v);
            }
        });
    }

    public void setRightNavigation(int imgResId, final OnRightClickListener onRightClickListener) throws NotFoundException {
        Drawable drawable = mContext.getResources().getDrawable(imgResId);
        if (null == drawable) throw new NotFoundException("thre resoruce id not found in drawable");
        ivNavigationHeaderRight.setVisibility(View.VISIBLE);
        ivNavigationHeaderRight.setImageResource(imgResId);
        rlNavigationRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onRightClickListener.onClick(v);
            }
        });
    }

    public void setRightBackgroundNavigation(int imgResId, final OnRightClickListener onRightClickListener) throws NotFoundException {
        Drawable drawable = mContext.getResources().getDrawable(imgResId);
        if (null == drawable) throw new NotFoundException("thre resoruce id not found in drawable");
        ivNavigationHeaderRight.setVisibility(View.VISIBLE);
        ivNavigationHeaderRight.setBackgroundResource(imgResId);
        rlNavigationRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onRightClickListener.onClick(v);
            }
        });
    }


    public void setShowLeft(boolean showLeft) {
        if (showLeft) {
            tvNavigationHeaderLeft.setVisibility(View.VISIBLE);
            ivNavigationHeaderLeft.setVisibility(View.VISIBLE);
        } else {
            tvNavigationHeaderLeft.setVisibility(View.GONE);
            ivNavigationHeaderLeft.setVisibility(View.GONE);
        }
    }

    public void setShowRight(boolean showRight) {
        if (showRight) {
            tvNavigationHeaderRight.setVisibility(View.VISIBLE);
            ivNavigationHeaderRight.setVisibility(View.VISIBLE);
        } else {
            tvNavigationHeaderRight.setVisibility(View.GONE);
            ivNavigationHeaderRight.setVisibility(View.GONE);
        }
    }

    public void setTitle(String title) {
        llTitle.setVisibility(View.VISIBLE);
        rlTitleSpinner.setVisibility(View.GONE);
        tvNaviationTitle.setText(title);
    }

    public void setTitle(int titleId) {
        llTitle.setVisibility(View.VISIBLE);
        rlTitleSpinner.setVisibility(View.GONE);
        tvNaviationTitle.setText(titleId);
    }

    public void setTitle(String title, final OnTitleClickListener titleClickListener) {
        llTitle.setVisibility(View.VISIBLE);
        rlTitleSpinner.setVisibility(View.GONE);
        tvNaviationTitle.setText(title);
        tvNaviationTitle.setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    titleClickListener.onClick(v);
                                                }
                                            }
        );
    }

    public void setTitleAsSpinner(String title, List<String> data, final OnSpinnerClickListener onSpinnerClickListener) {
        rlTitleSpinner.setVisibility(View.VISIBLE);
        llTitle.setVisibility(View.GONE);
        spDate.setVisibility(View.VISIBLE);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, R.layout.myspinner, data);
        spDate.setAdapter(adapter);
        spDate.setSelection(0, true);
        spDate.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View view, int position, long arg3) {
                onSpinnerClickListener.onItemClick(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }

        });
        rlTitleSpinner.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                spDate.performClick();
            }
        });
    }

    public void setShowTitleImage(boolean isShow) {
        if (isShow) {
            ivTitleImage.setVisibility(View.VISIBLE);
        } else {
            ivTitleImage.setVisibility(View.GONE);
        }
    }

    public void setBackgroundResourceX(int resId) {
        view.setBackgroundResource(resId);
    }

    public void setBackgroundColorX(int resId) {
        view.setBackgroundColor(resId);
    }

    public void setAlphaX(float alpha) {
        view.setAlpha(alpha);
    }

    public interface OnLeftClickListener {
        void onClick(View v);
    }

    public interface OnRightClickListener {
        void onClick(View v);
    }

    public interface OnTitleClickListener {
        void onClick(View v);
    }

    public interface OnSpinnerClickListener {
        void onItemClick(int postion);
    }


}
